<template>
  <div>
    <!--面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图-->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容"
                    v-model="queryInfo.query"
                    clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"/>
          </el-input>
        </el-col>
      </el-row>
      <el-table
        :data="orderlist" border stripe>
        <el-table-column type="index"/>
        <el-table-column prop="order_number" label="订单编号"/>
        <el-table-column prop="order_price" label="订单价格" width="100px"/>
        <el-table-column prop="pay_status" label="是否付款" width="90px">
          <template v-slot:="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="90px"/>
        <el-table-column prop="create_time" label="下单时间" width="160px">
          <template v-slot:="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"
                       @click="editOrderInfo(scope.row.order_number)">
            </el-button>
            <el-button type="success" icon="el-icon-location" size="mini"
                       @click="showProgressBox">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页插件-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="pageSizes"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
    <!--修改地址对话框-->
    <el-dialog
      title="修改地址"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1" class="cat_opt">
          <el-cascader :options="cityData"
                       :props="cascaderProps"
                       v-model="editForm.address1">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="">确 定</el-button>
      </span>
    </el-dialog>
    <!--物流地址进度对话框-->
    <el-dialog
      title="物流进度"
      :visible.sync="progressDialogVisible"
      width="50%"
      @close="progressDialogClosed"
    >
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressData"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import cityData from './citydata'
  import progressData from "./progressData";

  export default {
    name: "Order",
    data() {
      return {
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 5
        },
        orderlist: [],
        total: 0,
        pageSizes: [5, 10, 20, 50],
        addDialogVisible: false,
        editForm: {
          address1: [],
          address2: ''
        },
        editFormRules: {
          address1: [{required: true, message: '请选择省市区/县', trigger: 'blur'},
          ],
          address2: [{required: true, message: '请输入详细地址', trigger: 'blur'},
          ]
        },
        cityData,
        cascaderProps: {
          expandTrigger: 'hover',
          value: 'value',
          label: 'label',
          children: 'children',
        },
        progressDialogVisible: false,
        progressData
      }
    },
    created() {
      this.getOrderList()
    },
    methods: {
      getOrderList() {
        this.$store.dispatch('getOrdersList_', this.queryInfo).then(() => {
          if (this.$store.state.orders_state !== 200) {
            return this.$message.error("查询订单列表失败")
          }
          this.orderlist = this.$store.state.orders_list.goods
          this.total = this.$store.state.total
        })
      },
      //监听pagesize改变
      handleSizeChange(newPageSize) {
        this.queryInfo.pagesize = newPageSize
        this.getOrderList()
      },
      handleCurrentChange(newPageNum) {
        this.queryInfo.pagenum = newPageNum
        this.getOrderList()
      },
      editOrderInfo(id) {
        // this.$store.dispatch('getOrdersById_', id).then(() => {
        //   if (this.$store.state.orders_state !== 200) {
        //     return this.$message.error("查询订单失败")
        //   }
        //   this.editForm = this.$store.state.order
        // })
        this.addDialogVisible = true

      },
      addDialogClosed() {
        this.$refs.editFormRef.resetFields()
      },
      showProgressBox() {
        this.progressDialogVisible = true
      },
      progressDialogClosed() {

      }
    }
  }
</script>

<style lang="less" scoped>
  .el-cascader {
    width: 100%;
  }

  .cat_opt {
    margin: 15px 0;
    text-align: left;
  }

</style>
